//
//  CLTableViewViewController.m
//  CLPlayerDemo
//
//  Created by JmoVxia on 2017/8/2.
//  Copyright © 2017年 JmoVxia. All rights reserved.
//

#import "CLTableViewViewController.h"
#import "CLPlayerView.h"
#import "CLTableViewCell.h"
#import "CLModel.h"
#import "UIView+CLSetRect.h"
#import <SDWebImage/SDWebImage.h>

#import <Masonry/Masonry.h>

#import "BulletView.h"
#import "BulletManger.h"



static NSString *CLTableViewCellIdentifier = @"CLTableViewCellIdentifier";

@interface CLTableViewViewController () <UITableViewDelegate, UITableViewDataSource, CLTableViewCellDelegate, UIScrollViewDelegate>

/**tableView*/
@property (nonatomic, strong) UITableView *tableView;
/**数据源*/
@property (nonatomic, strong) NSMutableArray *arrayDS;
/**CLplayer*/
@property (nonatomic, weak) CLPlayerView *playerView;
/**记录Cell*/
@property (nonatomic, assign) UITableViewCell *cell;


@property(strong,nonatomic) NSString *p; //分页
@property(strong,nonatomic) NSString *max_p; // 最大分页
@property(assign,nonatomic) int a;

@property(assign,nonatomic) BOOL isbofan;

@property(assign,nonatomic)  int recordxuanzhong;

@property(assign,nonatomic)  BOOL shifoudianj;

@property(strong,nonatomic) UIButton *shutDownkaiqiBtn;


@property (nonatomic,strong) BulletManger *bulletManager;
@property(strong,nonatomic) NSMutableArray *allComments;
@property(strong,nonatomic) UIView *hongSeuiview;
@property(strong,nonatomic) UIButton *pingluBut;




@end

@implementation CLTableViewViewController
#pragma mark - 懒加载
/**数据源*/
//- (NSMutableArray *) arrayDS{
//    if (_arrayDS == nil){
//        _arrayDS = [[NSMutableArray alloc] init];
//        NSData *JSONData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Date" ofType:@"json"]];
//        NSArray *array = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingAllowFragments error:nil];
//        [array enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//            CLModel *model = [CLModel new];
//            [model setValuesForKeysWithDictionary:obj];
//            [self->_arrayDS addObject:model];
//        }];
//    }
//    return _arrayDS;
//}



/**tableView*/
- (UITableView *) tableView{
    if (_tableView == nil){
        _tableView = [[UITableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = RGB(0, 0, 0);
        _tableView.showsVerticalScrollIndicator = NO;
        [_tableView registerClass:[CLTableViewCell class] forCellReuseIdentifier:CLTableViewCellIdentifier];
        _tableView.pagingEnabled = YES;
    }
    return _tableView;
}





//- (NSMutableArray *)allComments {
//    if (!_allComments) {
//        _allComments = [NSMutableArray arrayWithObjects:
//                        @"1",
//                        @"2",
//                        @"3",
//                         @"4",
//                        @"5",
//                        @"6",
//                         @"7",
//                        @"8",
//                        @"我是一条弹幕我是一条弹幕2",
//                        @"弹幕3",
//                        @"我是一条弹幕1",
//                         @"我是",
//                        @"弹6", nil];
//    }
//    return _allComments;
//}


#pragma mark - 视图加载完毕
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //  self.a = 1 ;
    
    NSLog(@" hhh  %f",self.view.frame.size.height);
    
    self.view.backgroundColor = RGB(0, 0, 0);
    
    
    
    [self initUI];
    [self fasomngshiping];
    //   UITableView
    
    [self addrefreshControl];
    
    
    // 添加关闭开启按钮
    [self addShutDownkaiqin];
    
    
    
    //  [self.bulletManager start];
    
    
    
    
}


- (void)shuxingshuju  {
    
    [self fasomngshiping];
    
    
}


- (void)addkongjian  {
    
    [self.hongSeuiview removeFromSuperview];
    self.hongSeuiview = nil;
    
    
    self.hongSeuiview = [[UIView alloc]initWithFrame:CGRectMake(0, CF_StatusBarHeight+40, self.view.frame.size.width, 120)];
    self.hongSeuiview.backgroundColor = [UIColor clearColor];
    [self.view addSubview: self.hongSeuiview];
    
    self.bulletManager = [[BulletManger alloc] init];
    
    
    
    
    self.bulletManager.allComments = self.allComments;
    
    
    
    __weak CLTableViewViewController *myself = self;
    
    
    self.bulletManager.generateBulletBlock = ^(BulletView *bulletView){
        
//        NSLog(@"jjjj : %@ --- %ld",myself.hongSeuiview.subviews,myself.hongSeuiview.subviews.count);
        [myself addBulletView:bulletView];
        
        
        
        
    };
    
    
}




-(void)addBulletView:(BulletView *)bulletView{
    
    //    bulletView.frame = CGRectMake(CGRectGetWidth(self.view.frame)+50, 200+34 * bulletView.trajectory, CGRectGetWidth(bulletView.bounds), CGRectGetHeight(bulletView.bounds));
    //
    //    bulletView.backgroundColor = [UIColor yellowColor];
    //
    //    [self.view addSubview:bulletView];
    //
    //    [bulletView startAnimation];
    
    
    bulletView.frame = CGRectMake(CGRectGetWidth(self.view.frame)+50, 10+34 * bulletView.trajectory, CGRectGetWidth(bulletView.bounds), CGRectGetHeight(bulletView.bounds));
    
    bulletView.backgroundColor = [UIColor clearColor];
    
    //   [self.view addSubview:bulletView];
    
    [self.hongSeuiview addSubview:bulletView];
    
    [bulletView startAnimation];
    
    
    
}







- (void)addShutDownkaiqin {
    
    
    UIButton  *guanbi  = [UIButton buttonWithType:UIButtonTypeCustom];
    self.pingluBut = guanbi;
    guanbi.frame = CGRectMake(kScreenW-30-15, 10, 25, 25);
    //  self.recommendedBut.backgroundColor = [UIColor redColor];
    //     guanbi.backgroundColor = [UIColor redColor];
    //  [guanbi setTitle:@"开启" forState:UIControlStateNormal];
    //    [guanbi setImage:[UIImage imageNamed:@"danmu_kai"] forState:UIControlStateNormal];
    [guanbi setBackgroundImage:[UIImage imageNamed:@"danmu_kai"] forState:UIControlStateNormal];
    
    
    
    // [guanbi setTitle:@"关闭" forState:UIControlStateSelected];
    
    //  [guanbi setImage:[UIImage imageNamed:@"danmu_guan"] forState:UIControlStateSelected];
    
    [guanbi setBackgroundImage:[UIImage imageNamed:@"danmu_guan"] forState:UIControlStateSelected];
    [guanbi addTarget:self action:@selector(guanbiTap:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:guanbi];
    
    
    
    
}


- (void)guanbiTap:(UIButton *)sender {
    
    
    if (sender.selected == YES) {
        
        sender.selected = NO;
        [self.bulletManager stop];
        //   self.bulletManager.allComments = [@[@"123",@"333"] mutableCopy];
        
        
        
    } else {
        
        
        
        
        [self addkongjian];
        sender.selected = YES;
        
        [self.bulletManager start];
        
        
        
        
        
    }
    
    
}






// 添加刷新
- (void)addrefreshControl {
    
    //    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(underRefresh)];
    //    [self.tableView.mj_header beginRefreshing];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(onRefresh)];
    
    // 全部评论
    
}

- (void)underRefresh {
    DLog(@"精选");
    
    
    
    self.p  = @"1";
    
    self.arrayDS = [[NSMutableArray alloc]init];
    NSString *uid = [HWLocallyStoredData getLoginUserID];
    NSString *tString = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
    //  NSString *gidStr = [NSString stringWithFormat:@"%@",self.gameId];
    NSString *signStr = [NSString stringWithFormat:@"%@%@%@",uid,tString,@"fsd213ewdsadqwe2121213edsad"];
    NSString *signMD5 = [WMUtil md5HexDigest:signStr];
    
    
    NSString *type = @"0";
    
    NSDictionary *parametes = @{
        
        
        @"ct":@"app",
        @"ac":@"video",
        @"uid":uid,
        @"t" :tString,
        @"sign" :signMD5,
        @"type":type,
        @"p":@"1",
        @"ios" : @"1",
        @"member_id":[HWLocallyStoredData getCode],
        @"p" : self.p,
        
        
        
    };
    
    
    [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {
        
        
        
        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
        
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
        if ([codeStr intValue] == 1) {
            // [MBProgressHUD showMessage:@"收藏成功" toView:self.view];
            
            self.arrayDS  = [CLModel mj_objectArrayWithKeyValuesArray:response[@"data"]];
            
            
            [self.tableView reloadData];
            
            
            
            
            
            
            
        } else {
            
            
            // [MBProgressHUD showError:@"收藏失败"];
            [MBProgressHUD showError:response[@"message"]];
            
        }
        
        
        
    } failure:^(NSString *error) {
        
        
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
    }];
    
    
    
    
    
    
    
}



- (void)onRefresh  {
    
    
    if ([self.p intValue] > [self.max_p intValue] ) {
        
        [MBProgressHUD showError:@"请稍后在加载数据"];
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
        return ;
    }
    // self.arrayDS = [[NSMutableArray alloc]init];
    NSString *uid = [HWLocallyStoredData getLoginUserID];
    NSString *tString = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
    //  NSString *gidStr = [NSString stringWithFormat:@"%@",self.gameId];
    NSString *signStr = [NSString stringWithFormat:@"%@%@%@",uid,tString,@"fsd213ewdsadqwe2121213edsad"];
    NSString *signMD5 = [WMUtil md5HexDigest:signStr];
    
    
    NSString *type = @"0";
    
    NSDictionary *parametes = @{
        
        
        @"ct":@"app",
        @"ac":@"video",
        @"uid":uid,
        @"t" :tString,
        @"sign" :signMD5,
        @"type":type,
        @"p":self.p ? self.p:@"1",
        @"ios" : @"1",
        @"member_id":[HWLocallyStoredData getCode],
        
        
    };
    
    
    
    [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {
        
        
        
        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
        
        
        //  [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
        if ([codeStr intValue] == 1) {
            // [MBProgressHUD showMessage:@"收藏成功" toView:self.view];
            
            NSMutableArray  *muArr   = [CLModel mj_objectArrayWithKeyValuesArray:response[@"data"]];
            
            //  self.tableView.contentOffset = CGPointMake(0, self.arrayDS.count*self.view.frame.size.height);
            
            
            //  int a = self.arrayDS.count;
            
            for (int i =0 ; i < muArr.count; i++) {
                [self.arrayDS addObject:muArr[i]];
                
            }
            
            [self.tableView reloadData];
            
            
            //               self.tableView.contentSize =  CGSizeMake(kScreenW, self.tableView.frame.size.height*self.arrayDS.count);
            //               //   self.tableView.contentOffset =
            //
            //               NSIndexPath *index = [NSIndexPath indexPathForRow:a inSection:0];
            //
            //
            //             [self.tableView scrollToRowAtIndexPath:index atScrollPosition:UITableViewScrollPositionTop animated:YES];
            
            self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
            self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
            
            
        } else {
            
            
            // [MBProgressHUD showError:@"收藏失败"];
            [MBProgressHUD showError:response[@"message"]];
            
        }
        
        
        
    } failure:^(NSString *error) {
        
        
        
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [MBProgressHUD showError:@"收藏失败"];
        
    }];
    
    
    
    
    
    
    
    
    
    
}








- (void)fasomngshiping {
    
    
    
    self.arrayDS = [[NSMutableArray alloc]init];
    NSString *uid = [HWLocallyStoredData getLoginUserID];
    NSString *tString = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
    //  NSString *gidStr = [NSString stringWithFormat:@"%@",self.gameId];
    NSString *signStr = [NSString stringWithFormat:@"%@%@%@",uid,tString,@"fsd213ewdsadqwe2121213edsad"];
    NSString *signMD5 = [WMUtil md5HexDigest:signStr];
    
    NSString *type = @"0";
    NSDictionary *parametes = @{
        
        
        @"ct":@"app",
        @"ac":@"video",
        @"uid":uid,
        @"t" :tString,
        @"sign" :signMD5,
        @"type":type,
        @"p":@"1",
        @"ios" : @"1",
        @"member_id":[HWLocallyStoredData getCode],
        
        
    };
    
    
    [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {
        
        
        
        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
        
        if ([codeStr intValue] == 1) {
            // [MBProgressHUD showMessage:@"收藏成功" toView:self.view];
            
            self.arrayDS  = [CLModel mj_objectArrayWithKeyValuesArray:response[@"data"]];
            
            self.tableView.contentOffset = CGPointMake(0,0);
            
            
            [self.tableView reloadData];
            
            
            self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
            
            self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
            
            
            NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
            
            
            [self  quanbupinglu:index];
            
            [self bofangshiping];
            
            
        } else {
            
            
            // [MBProgressHUD showError:@"收藏失败"];
            [MBProgressHUD showError:response[@"message"]];
            
        }
        
        
        
    } failure:^(NSString *error) {
        
        
        [MBProgressHUD showError:@"收藏失败"];
        
    }];
    
    
    
    
    
    
}



// 播放第一个视频
- (void)bofangshiping  {
    
    
    
    NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
    
    
    CLTableViewCell *cell =    (CLTableViewCell *)[self.tableView cellForRowAtIndexPath:index];
    
    [self  cl_tableViewCellPlayVideoWithCell:cell];
    
    
    
}



- (void)viewDidAppear:(BOOL)animated {
    
    [super viewDidAppear:animated];
    
    
    
}


- (void)viewDidDisappear:(BOOL)animated {
    
    
    [_playerView destroyPlayer];
    _cell = nil;
    
}

//  关闭播放
- (void)closeplayView {
    
    [_playerView destroyPlayer];
    _cell = nil;
    
    
}


- (void)initUI{
    self.navigationItem.title = @"CLPlayer";
    self.automaticallyAdjustsScrollViewInsets = NO;
    //self.view.backgroundColor = [UIColor whiteColor];
    
    //    self.tableView.frame = CGRectMake(0, 0 , kScreenW, kScreenH);
    
    [self.view addSubview:self.tableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return  1;
    
}


#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arrayDS.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CLTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CLTableViewCellIdentifier forIndexPath:indexPath];
    
    cell.backgroundColor  = RGB(0, 0, 0);
    cell.model = self.arrayDS[indexPath.row];
    
    [cell.suspendedBut addTarget:self action:@selector(suspendedTap:) forControlEvents:UIControlEventTouchUpInside];
    cell.suspendedBut.tag = indexPath.row;
    
    
    
    if (indexPath.row %2 == 0) {
        cell.backgroundColor  = RGB(0, 0, 0);
    } else {
        
        cell.backgroundColor  = RGB(0, 0, 0);
    }
    
    
    cell.delegate = self;
    return cell;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    // return  300;
    
    NSLog(@"高度111 %f --- %f", self.view.frame.size.height,tableView.frame.size.height);
    //  return kScreenH;
    return  self.tableView.frame.size.height;
    
    //
    
    
}





- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSLog(@"ioioioi");
    
    
}



//在willDisplayCell里面处理数据能优化tableview的滑动流畅性，cell将要出现的时候调用

/*
 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
 CLTableViewCell * myCell = (CLTableViewCell *)cell;
 myCell.model = self.arrayDS[indexPath.row];
 //Cell开始出现的时候修正偏移量，让图片可以全部显示
 [myCell cellOffset];
 //第一次加载动画
 [[SDImageCache sharedImageCache] diskImageExistsWithKey:myCell.model.image completion:^(BOOL isInCache) {
 //        if (!isInCache) {
 //            //主线程
 //            CATransform3D rotation;//3D旋转
 //            rotation = CATransform3DMakeTranslation(0 ,50 ,20);
 //            //逆时针旋转
 //            rotation = CATransform3DScale(rotation, 0.8, 0.9, 1);
 //            rotation.m34 = 1.0/ -600;
 //            myCell.layer.shadowColor = [[UIColor blackColor]CGColor];
 //            myCell.layer.shadowOffset = CGSizeMake(10, 10);
 //            myCell.alpha = 0;
 //            myCell.layer.transform = rotation;
 //            [UIView beginAnimations:@"rotation" context:NULL];
 //            //旋转时间
 //            [UIView setAnimationDuration:0.6];
 //            myCell.layer.transform = CATransform3DIdentity;
 //            myCell.alpha = 1;
 //            myCell.layer.shadowOffset = CGSizeMake(0, 0);
 //            [UIView commitAnimations];
 //        }
 }];
 }
 
 
 */

//cell离开tableView时调用
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    //因为复用，同一个cell可能会走多次
    if ([_cell isEqual:cell]) {
        //区分是否是播放器所在cell,销毁时将指针置空
        [_playerView destroyPlayer];
        _cell = nil;
    }
}
#pragma mark - 点击播放代理
- (void)cl_tableViewCellPlayVideoWithCell:(CLTableViewCell *)cell{
    //记录被点击的Cell
    _cell = cell;
    //销毁播放器
    [_playerView destroyPlayer];
    CLPlayerView *playerView = [[CLPlayerView alloc] initWithFrame:CGRectMake(0, 0, cell.CLwidth, cell.CLheight)];
    _playerView = playerView;
    [cell.contentView addSubview:_playerView];
    //视频地址
    _playerView.url = [NSURL URLWithString:cell.model.url];
    
    [_playerView updateWithConfigure:^(CLPlayerViewConfigure *configure) {
        configure.topToolBarHiddenType = TopToolBarHiddenSmall;
        configure.autoRotate = NO;
        
        //        config.smallGestureControl = YES;
    }];
    //播放
    [_playerView playVideo];
    //返回按钮点击事件回调
    [_playerView backButton:^(UIButton *button) {
        NSLog(@"返回按钮被点击");
    }];
    //播放完成回调
    [_playerView endPlay:^{
        //销毁播放器
        [self->_playerView destroyPlayer];
        self->_playerView = nil;
        self->_cell = nil;
        NSLog(@"播放完成");
    }];
    
    self.isbofan = YES;
    
    // cell insertSubview:<#(nonnull UIView *)#> belowSubview:<#(nonnull UIView *)#>
    //   [cell.contentView sendSubviewToBack:cell.cardView];
    [cell.contentView  bringSubviewToFront:cell.cardView];
    [cell.contentView  bringSubviewToFront:cell.rightView];
    [cell.contentView  bringSubviewToFront:cell.suspendedBut];
    
    
    
}




#pragma mark - 滑动代理


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    
    
    
    
    
    NSLog(@"多少 %f", scrollView.contentOffset.y);
    // visibleCells 获取界面上能显示出来了cell
    NSArray<CLTableViewCell *> *array = [self.tableView visibleCells];
    //enumerateObjectsUsingBlock 类似于for，但是比for更快
    [array enumerateObjectsUsingBlock:^(CLTableViewCell * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj cellOffset];
        
        
    }];
}







#pragma mark - 布局
-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    //    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
    //      //  make.top.equalTo(self.navigationController.navigationBar.mas_bottom);
    //        make.top.mas_equalTo(0);
    //      // make.bottom.equalTo(self.view.mas_bottom).offset(-49);
    //         make.bottom.equalTo(self.view.mas_bottom).offset(0);
    //        make.width.equalTo(self.view);
    //        make.height.equalTo(self.view);
    //
    //    }];
    
    //   self.view.frame = CGRectMake(0, 0 , kScreenW, kScreenH-49);
    // self.tableView.frame = CGRectMake(0, 20 , kScreenW, kScreenH-TabBarHeight-StatusBarHeight);
    //
    
    
    
    self.tableView.frame = self.view.frame;
    NSLog(@"数据 ： %f",self.tableView.frame.size.height);
    
    
    
    // 736 667
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    
    
    
    
    
    
    
    
    [UIView animateWithDuration:0.8 animations:^{
        
        CGFloat  f =       self.tableView.contentOffset.y/(self.tableView.frame.size.height)+0.5;
        NSLog(@"jjjjjj : %f --- %f -----%f ", f ,scrollView.contentOffset.y,(self.tableView.frame.size.height));
        
        int  heig = (int)f;
        
        //    scrollView.contentOffset = CGPointMake(0, heig*(self.tableView.frame.size.height));
        
        NSLog(@"dd --- %f   ff --- %f",heig*(self.view.frame.size.height) , self.view.frame.size.height);
        
        
        NSArray<CLTableViewCell *> *array = [self.tableView visibleCells];
        
        CLTableViewCell *cell1  =    array.lastObject;
        
        if (self.tableView.contentOffset.y < 0) {
            cell1 =  array.firstObject;
        }
        
        
        
        NSIndexPath *index = [self.tableView indexPathForCell:cell1];
        
        
        NSLog(@"真正的   %ld",index.row);
        
        
        
        
        if (self.shifoudianj == YES  ) {
            
            
            
            CLTableViewCell *zuiqiancell  =    array.firstObject;
            NSIndexPath *zuiqiindex1 = [self.tableView indexPathForCell:zuiqiancell];
            
            
            [self.tableView scrollToRowAtIndexPath:zuiqiindex1 atScrollPosition:UITableViewScrollPositionTop animated:YES]; // 关键代码
            
            
            self.shifoudianj  = NO;
            
            return;
            
        }
        
        self.shifoudianj  = NO;
        
        //    self.recordxuanzhong = (int)index.row;
        
        
        [self.tableView scrollToRowAtIndexPath:index atScrollPosition:UITableViewScrollPositionTop animated:YES]; // 关键代码
        
        [self cl_tableViewCellPlayVideoWithCell:cell1];
        
        [self  quanbupinglu:index];
        
        
        
        
    }];
    
    
    
    
    
    
    
    
    //    [UIView animateWithDuration:0.8 animations:^{
    //
    //           CGFloat  f =       self.tableView.contentOffset.y/(self.tableView.frame.size.height)+0.5;
    //             NSLog(@"jjjjjj : %f --- %f -----%f ", f ,scrollView.contentOffset.y,(self.tableView.frame.size.height));
    //
    //           int  heig = (int)f;
    //
    //      //    scrollView.contentOffset = CGPointMake(0, heig*(self.tableView.frame.size.height));
    //
    //           NSLog(@"dd --- %f   ff --- %f",heig*(self.view.frame.size.height) , self.view.frame.size.height);
    //
    //
    //
    //
    //        if (self.arrayDS.count < heig) {
    //            return;
    //        }
    //
    //
    //           NSIndexPath *index = [NSIndexPath indexPathForRow:heig inSection:0];
    //
    //
    //        [self.tableView scrollToRowAtIndexPath:index atScrollPosition:UITableViewScrollPositionTop animated:YES]; // 关键代码
    //
    //       //  self.tableView.contentOffset
    //        NSLog(@"偏移量  %f",self.tableView.contentOffset.y);
    //
    //
    //
    //          CLTableViewCell  *cell = (CLTableViewCell *)[self.tableView cellForRowAtIndexPath:index];
    //
    //         //   playAction
    //
    //         //   [self cl_tableViewCellPlayVideoWithCell:cell];
    //
    //
    //    }];
    //
    //
    
    
    
    //    [UIView animateWithDuration:0.8 animations:^{
    //
    //        CGFloat  f =       scrollView.contentOffset.y/self.view.frame.size.height+0.5;
    //          NSLog(@"jjjjjj : %f --- %f -----%f ", f ,scrollView.contentOffset.y,kScreenH);
    //
    //        int  heig = (int)f;
    //
    //        scrollView.contentOffset = CGPointMake(0, heig*(kScreenH));
    //
    //        NSLog(@"dd --- %f   ff --- %f",heig*(self.view.frame.size.height) , self.view.frame.size.height);
    //
    //
    //
    //        NSIndexPath *index = [NSIndexPath indexPathForRow:heig inSection:0];
    //
    //
    //       CLTableViewCell  *cell = (CLTableViewCell *)[self.tableView cellForRowAtIndexPath:index];
    //
    //      //   playAction
    //
    //      //   [self cl_tableViewCellPlayVideoWithCell:cell];
    //
    //
    //    }];
    //
    
    //    [UIView animateWithDuration:0.8 animations:^{
    //
    //         CGFloat  f =       scrollView.contentOffset.y/(kScreenH -  CF_TabbarHeight)+0.5;
    //           NSLog(@"jjjjjj : %f", f);
    //
    //         int  heig = (int)f;
    //
    //         scrollView.contentOffset = CGPointMake(0, heig*(kScreenH -  CF_TabbarHeight));
    //
    //         NSIndexPath *index = [NSIndexPath indexPathForRow:heig inSection:0];
    //
    //
    //        CLTableViewCell  *cell = (CLTableViewCell *)[self.tableView cellForRowAtIndexPath:index];
    //
    //       //   playAction
    //
    //          [self cl_tableViewCellPlayVideoWithCell:cell];
    //
    //
    //     }];
    //
    
    
    
    
}




//- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
//    
//    [UIView animateWithDuration:0.8 animations:^{
//        
//        CGFloat  f =       scrollView.contentOffset.y/self.view.frame.size.height+0.5;
//          NSLog(@"jjjjjj : %f", f);
//        
//        int  heig = (int)f;
//        
//        scrollView.contentOffset = CGPointMake(0, heig*self.view.frame.size.height);
//        
//    }];
//    
//    
//}

//- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
//    
//    [UIView animateWithDuration:0.8 animations:^{
//        
//        CGFloat  f =       scrollView.contentOffset.y/self.view.frame.size.height+0.5;
//          NSLog(@"jjjjjj : %f", f);
//        
//        int  heig = (int)f;
//        
//        scrollView.contentOffset = CGPointMake(0, heig*self.view.frame.size.height);
//        
//    }];
//    
//    
//}


- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if (decelerate == YES) {
        NSLog(@"iioioioioio");
        
        scrollView.scrollEnabled = NO;
        
    }
    scrollView.scrollEnabled =  YES;
    NSLog(@"1234 iio1ioioioio");
    
    
}


-(void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    //   [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackTranslucent;
    
    
    
    
    
    
    //  UIStatusBarStyleDefault
    //    UIStatusBarStyleLightContent
}


-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
//    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    
    
}




// 点击暂停
- (void)suspendedTap:(UIButton *)sender  {
    
    
    NSLog(@"1234577");
    self.shifoudianj = YES;
    
    
    
    NSIndexPath *zuiqiindex1 =  [NSIndexPath indexPathForRow:sender.tag inSection:0];
    
    [self.tableView scrollToRowAtIndexPath:zuiqiindex1 atScrollPosition:UITableViewScrollPositionTop animated:YES]; // 关键代码
    
    
    
    
    if (self.playerView != nil) {
        
        if (self.isbofan ==  YES) {
            
            self.isbofan =  NO;
            [self.playerView pausePlay];
        } else {
            
            
            self.isbofan =  YES;
            [self.playerView playVideo];
            
        }
        
        
        
        
    }
    
    
    
    
}




- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    
    NSLog(@"111");
    
    
    
}



- (void)quanbupinglu:(NSIndexPath *) index {
    
    
    
    
    
    if (index == nil) {
        return;
    }
    
    [self.bulletManager stop];
    [self addkongjian];
    self.pingluBut.selected = NO;
    [self.bulletManager stop];
    
    
    
    
    CLModel *model =    self.arrayDS[index.row];
    
    
    
    //     self.dataMuArr = [[NSMutableArray alloc]init];
    
    
    //    NSString *uid = [HWLocallyStoredData getLoginUserID];
    NSString *tString = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
    //  NSString *gidStr = [NSString stringWithFormat:@"%@",self.gameId];
    NSString *signStr = [NSString stringWithFormat:@"%@%@",tString,@"fsd213ewdsadqwe2121213edsad"];
    
    //   NSString *signStr = [NSString stringWithFormat:@"%@%@%@",uid,tString,@"fsd213ewdsadqwe2121213edsad"];
    NSString *signMD5 = [WMUtil md5HexDigest:signStr];
    
    
    NSString *type = @"1";
    
    //           NSDictionary *parametes = @{
    //
    //
    //                             @"ct":@"app",
    //                             @"ac":@"video_pinglun",
    //                             @"uid":uid,
    //                             @"t" :tString,
    //                             @"sign" :signMD5,
    //                             @"id" : self.idStr,
    //                             @"ios" : @"1",
    //
    //           };
    
    NSDictionary *parametes = @{
        
        
        @"ct":@"app",
        @"ac":@"video_pinglun",
        @"t" :tString,
        @"sign" :signMD5,
        @"id" : model.id,
        @"member_id":[HWLocallyStoredData getCode],
        @"ios" : @"1",
        
    };
    
    
    // doGetWithParameters
    
    [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {
        
        
        
        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
        
        
        
        
        if ([codeStr intValue] == 1) {
            // [MBProgressHUD showMessage:@"收藏成功" toView:self.view];
            
            //                   self.dataMuArr  = [HWCommentsModel mj_objectArrayWithKeyValuesArray:response[@"data"]];
            
            NSArray *dataArr = response[@"data"];
            
            
            self.allComments = [[NSMutableArray alloc]init];
            
            
            for (int i = 0; i < dataArr.count; i++) {
                
                NSDictionary *dic = dataArr[i];
                [self.allComments addObject:dic[@"content"]];
                
                
            }
            
            if (self.allComments.count > 0 ) {
                
                
                [self addkongjian];
                self.pingluBut.selected = YES;
                
                [self.bulletManager start];
                
            } else {
                
                [self.bulletManager stop];
                [self addkongjian];
                self.pingluBut.selected = NO;
                [self.bulletManager stop];
            }
            
            
            
            //
            //                   [self.mainaTableVie reloadData];
            //
            //
            
            
        } else {
            
            
            // [MBProgressHUD showError:@"收藏失败"];
            [MBProgressHUD showError:response[@"message"]];
            
        }
        
        
        
    } failure:^(NSString *error) {
        
        
        
        
        [MBProgressHUD showError:@"请求失败"];
        
    }];
    
    
    
    
    
    
    
    
}


- (void)kaipianViewcl_tableViewCellPlayVideoWithCell:(CLTableViewCell *)cell {
    
    
    
    
    
    
    //    NSIndexPath *zuiqiindex1 =  [NSIndexPath indexPathForRow:sender.tag inSection:0];
    NSIndexPath *zuiqiindex1 =  [self.tableView indexPathForCell:cell];
    
    
    [self.tableView scrollToRowAtIndexPath:zuiqiindex1 atScrollPosition:UITableViewScrollPositionTop animated:YES]; // 关键代码
    
    
    
    
}


@end
